package fr.univ.surveyonline.question.xmlbuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import fr.univ.surveyonline.datastore.DatastoreKeyResponse;
import fr.univ.surveyonline.question.*;
import fr.univ.surveyonline.question.xmlbuilder.balises.ChoiceBalise;
import fr.univ.surveyonline.question.xmlbuilder.balises.ChoicesBalise;
import fr.univ.surveyonline.question.xmlbuilder.balises.CompositeFieldBalise;
import fr.univ.surveyonline.question.xmlbuilder.balises.DescriptionBalise;
import fr.univ.surveyonline.question.xmlbuilder.balises.FormFieldBalise;
import fr.univ.surveyonline.question.xmlbuilder.balises.QuestionBalise;

public class toXML {

	private static String getDebut(String baliseValue,Map<String,String> attributes) {
		String s = "<" + baliseValue;
		
		if (attributes != null) {
			for (String at : attributes.keySet()) {
				s += " " + at + "=\"" + attributes.get(at) + "\"";
			}
		}
		
		return s + ">";
	}
	
	private static String getEnd(String baliseValue) {
		return "</" + baliseValue + ">";
	}
	
	public static String fromFormFieldToXML(FormField f) {
		String s = getDebut(FormFieldBalise.BALISE_VALUE,null);
		
		s += describeFormField(f);
		
		return s + getEnd(FormFieldBalise.BALISE_VALUE);
	}

	private static String describeFormField(FormField f) {
		Iterator<ICompositeField> it = f.getIterator();
		
		String s = "";
		while (it.hasNext()) {
			ICompositeField cf = it.next();
			Map<String,String> map = new HashMap<String,String>();
			map.put(CompositeFieldBalise.KEY, cf.getKey().toString());
			map.put(CompositeFieldBalise.FIELD_TYPE, cf.getClass().getSimpleName());
			s += getDebut(CompositeFieldBalise.BALISE_VALUE,map);
			
			s += getDebut(QuestionBalise.BALISE_VALUE,null);
			s += cf.getQuestion().getText();
			s += getEnd(QuestionBalise.BALISE_VALUE);

			if (cf.getDescription() != null) {
				s += getDebut(DescriptionBalise.BALISE_VALUE,null);
				s += cf.getDescription().getText();
				s += getEnd(DescriptionBalise.BALISE_VALUE);
			}
			
			if (cf.getType() != TypeCompositeField.TEXT_FIELD) {
				IChoiceField ch = (IChoiceField) cf;
				
				s += getDebut(ChoicesBalise.BALISE_VALUE,null);
				
				for (DatastoreKeyResponse k : ch.getAllKey()) {
					Map<String,String> map2 = new HashMap<String,String>();
					map2.put(ChoiceBalise.KEY, k.toString());
					
					s += getDebut(ChoiceBalise.BALISE_VALUE,map2);
					s += ch.getAnswer(k);
					s += getEnd(ChoiceBalise.BALISE_VALUE);
				}
				
				s += getEnd(ChoicesBalise.BALISE_VALUE);
			}
			
			if (cf.getType() == TypeCompositeField.MULTIPLE_SUCCESSOR_FIELD) {
				IMultipleSuccessorField ch = (IMultipleSuccessorField) cf;
				
				for (DatastoreKeyResponse k : ch.getAllKey()) {
					if (ch.getChildren(k) != null) {
						Map<String,String> map2 = new HashMap<String,String>();
						map2.put(FormFieldBalise.GROUP, k.toString());
						
						s += getDebut(FormFieldBalise.BALISE_VALUE,map2);
						s += describeFormField(ch.getChildren(k));
						s += getEnd(FormFieldBalise.BALISE_VALUE);
					}
				}				
			}
			
			s += getEnd(CompositeFieldBalise.BALISE_VALUE);
		}
		return s;
	}
}
